$(function() {
   
    $('.input_text').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
    $('textarea').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
           
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });

    $("#btn_invite_guest").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (checkValidate('error-msg')) {
                $("#btn_invite_guest").attr("disabled", "disabled");
                $.post(localStorage.domain_app+"/webservice/guest/finalStepInviteGuest.php", {
                    user_id: localStorage.userID,
                    access_token: localStorage.facebook_token,
                    event_id: sessionStorage.select_event_id,
                    guest_list_from_fb: sessionStorage.guest_list_from_fb,
                    guest_list_from_email: sessionStorage.guest_list_from_email,
                    subject: $('input[name="subject"]').val(),
                    message: $('textarea').val()
                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;

                    } else {
                        print_alert(res.msg);
                    }
                    if (msgEr === '') {
                        if(sessionStorage.invite_guest_back==="event_detail"){
                            wd_redirect("eventDetail.html");
                        }else{
                            wd_redirect("selectEvent.html"); 
                        }
                        

                    } else {
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    $("#btn_invite_guest").removeAttr("disabled");
                }, "json");
            }
        } else {
            return false;
        }
    });
});
function checkValidate(id_append) {
// error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please enter %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="subject"]').val() == '' || $('input[name="subject"]').val() =='Subject') {
        msgEr = errormsg[2].replace('%s', 'subject');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="subject"]'), num_milliseconds);
        return false;
    } 
    if ($('textarea').val() == '' || $('textarea').val() == 'Type message here') {
        msgEr = errormsg[2].replace('%s', 'message');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('textarea'), num_milliseconds);
        return false;
    }    
    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append,  num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}